<?php
namespace app\Common\model;
use app\common\model\BaseModel;
/**
 * 管理员模型
 * Class AdminModel
 * @package app\Common\model
 */
class AdminConfigModel extends BaseModel
{
    protected $name = 'admin_config';

    /**
     * 更新或插入数据
     * @param $k 字段
     * @param $v 值
     */
    public static function saveFiled($k, $v){
        $find = self::where(['key'=>$k])->find();
        $v=htmlspecialchars(trim($v));
        if($find){
            return self::where(['key'=>$k])->save(['value'=>$v]);
        }else{
            return self::insert(['key'=>$k,'value'=>$v]);
        }
    }

    /**
     * 查询
     * @param array $where 提交
     * @return array
     */
    public static function selectAll($where = []){
        $config = self::where($where)->select();
        $re = [];
        foreach ($config as $key=>$value){
            $re[$value['key']] = $value['value'];
        }

        return $re ? $re : [];
    }

    /**
     * 此方法不建议使用
     * @param $data
     * @return bool
     */
    public static function saveInAll($data)
    {
        foreach ($data as $key=>$value){

           self::saveFiled($value['key'],$value['value']);
        }

        return  true;
    }


}
